package com.ecoolex.hotel.saas.job.scheduler;

import java.util.Calendar;
import java.util.List;

import com.ecoolex.hotel.saas.enums.PresaleCouponOrderPaymentType;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecoolex.framework.scheduler.Job;
import com.ecoolex.framework.scheduler.JobComponent;
import com.ecoolex.hotel.saas.core.manager.PaymentOrderManager;
import com.ecoolex.hotel.saas.core.service.WeChatPayService;
import com.ecoolex.hotel.saas.entity.PaymentOrder;

@JobComponent
public class PaymentOrderCheckJob {

	@Autowired
	private PaymentOrderManager paymentOrderManager;
	
	@Autowired
	private WeChatPayService weChatPayService;
	
	@Job(name = "hotel.saas.PaymentOrderCheckJob.checkPayResult", describe = "检查支付单的支付状态")
	public void checkPayResult() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, -5);
		List<PaymentOrder> list = paymentOrderManager.queryPayApplyTimeOut(calendar.getTime(), 10);
		list.forEach(paymentOrder -> {
			if(PresaleCouponOrderPaymentType.WECHAT_PAY.real().equals(paymentOrder.getPaymentType())) {
				weChatPayService.checkPayStatus(paymentOrder, PaymentOrderCheckJob.class.getSimpleName());
			}
		});
	}
	
	@Job(name = "hotel.saas.PaymentOrderCheckJob.checkRefundResult", describe = "检查支付单的退款状态")
	public void checkRefundResult() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, -5);
		List<PaymentOrder> list = paymentOrderManager.queryRefundApplyTimeOut(calendar.getTime(), 10);
		list.forEach(paymentOrder -> {
			if(PresaleCouponOrderPaymentType.WECHAT_PAY.real().equals(paymentOrder.getPaymentType())) {
				weChatPayService.checkRefundStatus(paymentOrder, PaymentOrderCheckJob.class.getSimpleName());
			}
		});
	}
}
